package com.bluefox.system.quartz;

import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 孙峰
 * @Description:    @PersistJobDataAfterExecution // 多次调用这个  都会保存数据信息  count的数据才会累加
 *                                     注意有状态信息会存放在jobDetail中
 * @date 2020/12/1 14:27
 */
@Slf4j
@PersistJobDataAfterExecution //  加上这注解 多次调用这个  都会保存数据信息  count的数据才会累加
public class DemoJob extends QuartzJobBean {


    private Integer count;

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        LocalDateTime localDateTime = LocalDateTime.now();
        log.info(localDateTime.format(DateTimeFormatter.ISO_DATE_TIME));
        JobDetail jobDetail = context.getJobDetail();
        log.info(jobDetail.getKey().getName());
        Trigger trigger = context.getTrigger();
        log.info(trigger.getKey().getName());

        System.out.println(context.getJobDetail().getJobDataMap().get("message"));
        System.out.println(context.getTrigger().getJobDataMap().get("message"));

        ++count;
        System.out.println(count);
        context.getJobDetail().getJobDataMap().put("count",count);
        System.out.println(context.getNextFireTime());
    }
}
